package com.bookpavilion.sms.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SmsUtil {

    @Value("${tencent-cloud.secretId}")
    private String secretId;
    @Value("${tencent-cloud.secretKey}")
    private String secretKey;
    @Value("${tencent-cloud.sdkAppid}")
    private String sdkAppid;
    @Value("${tencent-cloud.templateID}")
    private String templateID;
    @Value("${tencent-cloud.signContent}")
    private String signContent;
    public SendSmsResponse sendSms(String phone, String code){
        Credential cred = new Credential(secretId,
                secretKey);
        // 实例化要请求产品(以cvm为例)的client对象
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
        SmsClient smsClient = new SmsClient(cred, "ap-guangzhou");//第二个ap-chongqing 填产品所在的区
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSmsSdkAppid(sdkAppid);//appId ,见《创建应用》小节
        String[] phones = {"+86"+phone};  //发送短信的目标手机号，可填多个。
        sendSmsRequest.setPhoneNumberSet(phones);
        sendSmsRequest.setTemplateID(templateID);  //模版id,见《创建短信签名和模版》小节
        String[] templateParam = {code};//模版参数，从前往后对应的是模版的{1}、{2}等,见《创建短信签名和模版》小节
        sendSmsRequest.setTemplateParamSet(templateParam);
        sendSmsRequest.setSign(signContent); //签名内容，不是填签名id,见《创建短信签名和模版》小节
        try {
            SendSmsResponse sendSmsResponse = smsClient.SendSms(sendSmsRequest); //发送短信
            System.out.println(sendSmsResponse.toString());
            System.out.println(SendSmsResponse.toJsonString(sendSmsResponse));
            return sendSmsResponse;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return null;
    }
}
